﻿@{
    ViewBag.Title = "jqGrid in ASP.NET MVC - Searching [Advanced]";

    var grid = new Lib.Web.Mvc.JQuery.JqGrid.JqGridHelper<jqGrid.Models.ProductSearchableViewModel>("products",
        dataType: Lib.Web.Mvc.JQuery.JqGrid.JqGridDataTypes.Json,
        methodType: Lib.Web.Mvc.JQuery.JqGrid.JqGridMethodTypes.Post,
        pager: true,
        rowsNumber: 10,
        sortingName: "Id",
        sortingOrder: Lib.Web.Mvc.JQuery.JqGrid.JqGridSortingOrders.Asc,
        url: Url.Action("Products"),
        viewRecords: true
    ).Navigator(new Lib.Web.Mvc.JQuery.JqGrid.JqGridNavigatorOptions() { Add = false, Delete = false, Edit = false, View = false },
        searchActionOptions: new Lib.Web.Mvc.JQuery.JqGrid.JqGridNavigatorSearchActionOptions() {
            AdvancedSearching = true,
            CloneSearchRowOnAdd = false,
            Templates = new Dictionary<string, Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilters>() {
                { "Beverages and Condiments", new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilters() {
                    GroupingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchGroupingOperators.Or,
                    Filters = new List<Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter>() {
                        new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter() { SearchingName = "Category", SearchingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchOperators.Eq, SearchingValue = "1" },
                        new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter() { SearchingName = "Category", SearchingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchOperators.Eq, SearchingValue = "2" }
                    }                                                                            
                }},
                { "Bigfoot Breweries Beverages", new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilters() {
                    GroupingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchGroupingOperators.And,
                    Filters = new List<Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter>() {
                        new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter() { SearchingName = "SupplierId", SearchingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchOperators.Eq, SearchingValue = "16" },
                        new Lib.Web.Mvc.JQuery.JqGrid.JqGridRequestSearchingFilter() { SearchingName = "Category", SearchingOperator = Lib.Web.Mvc.JQuery.JqGrid.JqGridSearchOperators.Eq, SearchingValue = "1" }
                    }                                                                            
                }}
            }
        });
}

@grid.GetHtml()
<div id="supplier"></div>

@section JavaScript {
    <script id="supplier-tmpl" type="text/x-jquery-tmpl">
        ${CompanyName}<br /><br />
        ${Address}<br />
        ${PostalCode}, ${City}<br />
        ${Country}<br /><br />
        ${Phone}<br />
        ${HomePage}
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            @grid.GetJavaScript()
            $('#supplier').dialog({ autoOpen: false, bgiframe: true, resizable: false, title: 'Supplier' });
            $(document).on('click', 'a[data-supplier-id]', function (e) {
                if (e.preventDefault)
                    e.preventDefault();
                else
                    e.returnValue = false;

                var dialogPosition = $(this).offset();
                $.post('@Url.Action("Supplier")', { id: $(this).attr('data-supplier-id') }, function(data) {
                    $('#supplier').empty();
                    $('#supplier-tmpl').tmpl(data).appendTo('#supplier');
                    $('#supplier').dialog('option', 'position', [dialogPosition.left, dialogPosition.top]);
                    $('#supplier').dialog('open');
                });
            });
        });

        $.unitsInStockFormatter = function(cellvalue, options, rowObject) {
            var cellValueInt = parseInt(cellvalue);
            if (cellValueInt > 10)
                return "<img src='@Url.Content("~/Content/images/ui-flag_green.png")' alt='" + cellvalue + "' title='" + cellvalue + "' />";
            else if (cellValueInt > 0)
                return "<img src='@Url.Content("~/Content/images/ui-flag_blue.png")' alt='" + cellvalue + "' title='" + cellvalue + "' />";
            else
                return "<img src='@Url.Content("~/Content/images/ui-flag_red.png")' alt='" + cellvalue + "' title='" + cellvalue + "' />";
        };

        $.supplierFormatter = function(cellvalue, options, rowObject) {
            return "<a href='#' data-supplier-id='" + cellvalue.substr(1, cellvalue.indexOf(']') - 1) + "'>" + cellvalue.substr(cellvalue.indexOf(']') + 2) + "</a>";
        };
    </script>
}